package com.lm.service.impl;

import com.lm.dao.KwChannelDao;
import com.lm.dao.KwChannelDataDao;
import com.lm.model.KwChannel;
import com.lm.model.KwChannelData;
import com.lm.service.KwChannelDataService;
import com.meirengu.service.impl.BaseServiceImpl;
import com.meirengu.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * KwChannelData服务实现层
 *
 * @author Mao
 * @create Sat May 05 16:32:18 CST 2018
 */
@Service
public class KwChannelDataServiceImpl extends BaseServiceImpl<KwChannelData> implements KwChannelDataService {

    @Autowired
    KwChannelDao kwChannelDao;

    @Autowired
    private KwChannelDataDao kwChannelDataDao;

    /**
     * 申请产品
     *
     * @param channelId
     * @return
     */
    public int updateApplyNum(String channelId, boolean O2Oflag) {
        //异步更新渠道信息
        //查询当日记录
        Date currentDate = new Date();
        //当天开始时间
        Date dayBeginTime = DateUtils.getDayBeginTime(currentDate);
        //当天结束时间
        Date dayEndTime = DateUtils.getDayEndTime(currentDate);
        Map<String, Object> map = new HashMap<>();
        map.put("channelId", channelId);
        map.put("createDateBegin", dayBeginTime);
        map.put("createDateEnd", dayEndTime);
        List<Map<String, Object>> chaDataList = getList(map);
        KwChannelData chaData = null;
        if (chaDataList != null && chaDataList.size() > 0) {
            for (Map<String, Object> chaDataMap : chaDataList) {
                if (StringUtils.isNotBlank(chaDataMap.get("id").toString())) {
                    chaData = new KwChannelData();
                    chaData.setId(chaDataMap.get("id").toString());
                    chaData.setApplyNum(((int) chaDataMap.get("applyNum")) + 1);
                    if (O2Oflag) {
                        chaData.setO2oNum(((int) chaDataMap.get("O2ONum")) + 1);
                    }
                    return update(chaData);
                }
            }
        } else {
            //查询渠道
            KwChannel kwChannel = kwChannelDao.detail(channelId);
            if (kwChannel == null) return 0;
            chaData = new KwChannelData();
            chaData.setCreateName(kwChannel.getStaffAccount());
            chaData.setCreateDate(currentDate);
            chaData.setChannelId(kwChannel.getId());
            chaData.setVisitNum(0);
            chaData.setRegistNum(0);
            chaData.setApplyNum(1);
            chaData.setO2oNum(0);
            if (O2Oflag) {
                chaData.setO2oNum(1);
            }
            chaData.setChannelCode(kwChannel.getChannelCode());
            chaData.setChannelName(kwChannel.getChannelName());
            chaData.setRegCutRate(kwChannel.getRegCutRate());
            chaData.setRegCutRateBase(kwChannel.getRegCutRateBase());
            chaData.setApplyCutRate(kwChannel.getApplyCutRate());
            chaData.setApplyCutRateBase(kwChannel.getApplyCutRateBase());
            return insert(chaData);
        }
        return 0;
    }

    @Override
    public int addNum(Map map) {
        return kwChannelDataDao.addNum(map);
    }

}
